﻿using AutoMapper;
using EFEntity;
using Iservices;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
   public class engage_interviewServices : Iengage_interviewServices
    {
        private readonly MyDbContext myDBContext;
        private readonly IMapper mapper;

        public engage_interviewServices(MyDbContext myDBContext, IMapper mapper)
        {
            this.myDBContext = myDBContext;
            this.mapper = mapper;
        }

        public async Task<List<engage_interview>> FenYe(FenYe fy)
        {
            var data = myDBContext.engage_interview.OrderBy(e => e.ein_id).ToList();
            fy.Rows = data.Count();
            fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
            List<Eengage_interview> list = new List<Eengage_interview>();
            await Task.Run(() =>
            {
                list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
                    .Take(fy.PageSize)
                    .ToList();
            });
            //把EO-DTO
            List<engage_interview> list2 = new List<engage_interview>();
            foreach (Eengage_interview item in list)
            {
                engage_interview t = new engage_interview()
                {
                    ein_id = item.ein_id,
                    human_name = item.human_name,
                    interview_amount = item.interview_amount,
                    human_major_kind_id = item.human_major_kind_id,
                    human_major_kind_name = item.human_major_kind_name,
                    human_major_id = item.human_major_id,
                    human_major_name = item.human_major_name,
                    image_degree = item.image_degree,
                    native_language_degree = item.native_language_degree,
                    foreign_language_degree = item.foreign_language_degree,
                    response_speed_degree = item.response_speed_degree,
                    EQ_degree = item.EQ_degree,
                    IQ_degree = item.IQ_degree,
                    multi_quality_degree = item.multi_quality_degree,
                    register = item.register,
                    checker = item.checker,
                    registe_time = item.registe_time,
                    check_time = item.check_time,
                    resume_id = item.resume_id,
                    result = item.result,
                    interview_comment = item.interview_comment,
                    check_comment = item.check_comment,
                    interview_status = item.interview_status,
                    check_status = item.check_status,
                };
                list2.Add(t);
            }
            return list2;
        }

        public async Task<List<engage_interview>> getOne_interview_Async(int id)//查单个面试表信息
        {
            List<Eengage_interview> item = new List<Eengage_interview>();
            await Task.Run(() => { item = (from e in myDBContext.engage_interview where e.ein_id == id select e).ToList(); });
            List<engage_interview> t = mapper.Map<List<engage_interview>>(item);
            return t;
        }

        public async Task<List<engage_interview>> getOne_interview_AsyncE(int id)//查单个面试表信息
        {
            List<Eengage_interview> item = new List<Eengage_interview>();
            await Task.Run(() => { item = (from e in myDBContext.engage_interview where e.resume_id == id select e).ToList(); });
            List<engage_interview> t = mapper.Map<List<engage_interview>>(item);
            return t;
        }

        public async Task<int> update_ms_interview_status_Async(int id, int status)//修改面试表的面试状态
        {
            int result = 0;
            await Task.Run(() => {
                string sql = $" update [dbo].[engage_interview] set interview_status = {status} where [resume_id] = '{id}' ";
                result = SqlHelper.ZSG(sql, "engage_interviewServices");
            });
            return result;
        }

        public async Task<Dictionary<string, object>> FenYe_interview_Async(int currentpage, string where)//分页查询简历筛选
        {
            List<Eengage_interview> list = new List<Eengage_interview>();
            Dictionary<string, object> di = new Dictionary<string, object>();
            //调用分页存储过程
            SqlParameter[] ps = {
                new SqlParameter() {ParameterName="@pageSize",Value=3 },
                new SqlParameter() {ParameterName="@keyName",Value="ein_id" },
                new SqlParameter() {ParameterName="@tabelName",Value="engage_interview" },
                new SqlParameter() {ParameterName="@where",Value=where },
                new SqlParameter() {ParameterName="@currentPage",Value=currentpage },
               // new SqlParameter() {ParameterName="@lie",Value="*" },
                new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            list = await myDBContext.engage_interview.FromSqlRaw
                ("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps)
                     .ToListAsync();
            int rows = (int)ps[5].Value;
            int pages = (int)ps[6].Value;
            List<engage_interview> list2 = mapper.Map<List<engage_interview>>(list);
            di["list"] = list2;
            di["rows"] = rows;
            di["pages"] = pages;
            return di;
        }

        public async Task<int> update_ms_Async(engage_interview t)//修改面试筛选
        {
            int result = 0;
            Eengage_interview t2 = myDBContext.engage_interview.Find(t.ein_id);
            t2.interview_status = t.interview_status;
            t2.check_time = t.check_time;
            t2.checker = t.checker;
            t2.check_comment = t.check_comment;
            result = await myDBContext.SaveChangesAsync();
            return result;
        }

        public async Task<List<engage_interview>> SelectAll()
        {
            List<Eengage_interview> list = new List<Eengage_interview>();
            await Task.Run(() =>
            {
                list = myDBContext.engage_interview.ToList();
            });

            List<engage_interview> list2 = mapper.Map<List<engage_interview>>(list);
            return list2;
        }

        public async Task<engage_interview> SelectID(int id)
        {
            Eengage_interview list = new Eengage_interview();
            await Task.Run(() =>
            {
                list = myDBContext.engage_interview.Where(e => e.ein_id == id).AsNoTracking().FirstOrDefault();
            });
            engage_interview list2 = mapper.Map<engage_interview>(list);
            return list2;
        }

        public async Task<engage_interview> SelWhere(int id)
        {
            Eengage_interview list = new Eengage_interview();
            await Task.Run(() =>
            {
                list = myDBContext.engage_interview.Where(e => e.resume_id == id).AsNoTracking().FirstOrDefault();
            });
            engage_interview list2 = mapper.Map<engage_interview>(list);
            return list2;
        }

        public async Task<int> Update(engage_interview t)
        {
            Eengage_interview sks = new Eengage_interview()
            {
                interview_amount = t.interview_amount,
                checker = t.checker,
                check_comment = t.check_comment,
                check_status = t.check_status,
                check_time = t.check_time,
                EQ_degree = t.EQ_degree,
                foreign_language_degree = t.foreign_language_degree,
                human_major_id = t.human_major_id,
                human_major_kind_id = t.human_major_kind_id,
                human_major_kind_name = t.human_major_kind_name,
                human_major_name = t.human_major_name,
                human_name = t.human_name,
                image_degree = t.image_degree,
                interview_comment = t.interview_comment,
                interview_status = t.interview_status,
                IQ_degree = t.IQ_degree,
                multi_quality_degree = t.multi_quality_degree,
                native_language_degree = t.native_language_degree,
                register = t.register,
                registe_time = t.registe_time,
                response_speed_degree = t.response_speed_degree,
                result = t.result,
                resume_id = t.resume_id,
                ein_id = t.ein_id
            };

            myDBContext.engage_interview.Attach(sks);
            myDBContext.Entry(sks).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return await myDBContext.SaveChangesAsync();
        }
    }
}
